﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;

namespace WebApplication1.Admin
{
    public partial class QLDonHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                adddl();
                GetDH();
                adddl();
                
            }
        }
        List<BLLDonHang> dh = new List<BLLDonHang>();
        //load ds don hang mac dinh sap xep theo ngay
        private void GetDH()
        {
            Sort_Ngay(ddltanggiam.SelectedValue.ToString());

        }
        //link den trang chi tiet
        protected void GvDonHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "chitiet")
            {
                Response.Redirect("ChiTietDonHang.aspx?id="+e.CommandArgument+"");
            }
        }
        private void adddl()
        {
            ddltungay.DataSource = dh;
            ddltungay.DataTextField = "Ngay";
            ddltungay.DataValueField = "Ngay";
            ddltungay.DataBind();
            ddltungay.Items.Insert(0, new ListItem("[Chọn ngày]", "0"));
            ddltungay.SelectedIndex = 0;

            ddldenngay.DataSource = dh;
            ddldenngay.DataTextField = "Ngay";
            ddldenngay.DataValueField = "Ngay";
            ddldenngay.DataBind();
            ddldenngay.Items.Insert(0, new ListItem("[Chọn ngày]", "0"));
            ddldenngay.SelectedIndex = 0;
        }
        protected void Btncapnhat_Click(object sender, EventArgs e)
        {
            //neu chon. sap xep theo thanh tien
            if (Ddlxapxep.SelectedValue.ToString() =="2")
            {
                Sort_ThanhTien(ddltanggiam.SelectedValue.ToString());
                adddl();
            }
            //neu chon sap xep theo ngay
            if (Ddlxapxep.SelectedValue.ToString() == "1")
            {
                Sort_Ngay(ddltanggiam.SelectedValue.ToString());
                adddl();
            }
            //neu chon trang thay
            if (Ddlxapxep.SelectedValue.ToString() == "3")
            {
                Sort_TrangThai(ddltanggiam.SelectedValue.ToString());
                adddl();
            }
        }
        //sap xep theo gia tien
        private void Sort_ThanhTien(string chieu)
        {
            //List<BLLDonHang> dh = new List<BLLDonHang>();
            getdhbyngay();
            if (chieu == "1")
            {
                dh.Sort(delegate(BLLDonHang a, BLLDonHang b)
                    {
                        return a.TongTien.CompareTo(b.TongTien);
                    });
            }
            else
            {
                dh.Sort(delegate(BLLDonHang a, BLLDonHang b)
                {
                    return a.TongTien.CompareTo(b.TongTien)*-1;
                });  
            }
            GvDonHang.DataSource = dh;
            GvDonHang.DataBind();
        }
        //sap xep theo ngay
        private void Sort_Ngay(string chieu)
        {
            
            //List<BLLDonHang> dh = new List<BLLDonHang>();

            getdhbyngay();
            if (chieu == "1")
            {
                dh.Sort(delegate(BLLDonHang a, BLLDonHang b)
                {
                    return a.Ngay.CompareTo(b.Ngay);
                });
            }
            else
            {
                dh.Sort(delegate(BLLDonHang a, BLLDonHang b)
                {
                    return a.Ngay.CompareTo(b.Ngay) * -1;
                });
            }
            GvDonHang.DataSource = dh;
            GvDonHang.DataBind();
        }
        //sap xep theo trang thai
        private void Sort_TrangThai(string chieu)
        {

            getdhbyngay();
            if (chieu == "1")
            {
                dh.Sort(delegate(BLLDonHang a, BLLDonHang b)
                {
                    return a.Trangthai.CompareTo(b.Trangthai);
                });
            }
            else
            {
                dh.Sort(delegate(BLLDonHang a, BLLDonHang b)
                {
                    return a.Trangthai.CompareTo(b.Trangthai) * -1;
                });
            }
            GvDonHang.DataSource = dh;
            GvDonHang.DataBind();
        }
        //phan trang
        protected void GvDonHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GvDonHang.PageIndex = e.NewPageIndex;
            //neu chon. sap xep theo thanh tien
            if (Ddlxapxep.SelectedValue.ToString() == "2")
            {
                Sort_ThanhTien(ddltanggiam.SelectedValue.ToString());
            }
            //neu chon sap xep theo ngay
            if (Ddlxapxep.SelectedValue.ToString() == "1")
            {
                Sort_Ngay(ddltanggiam.SelectedValue.ToString());
            }
            //neu chon trang thay
            if (Ddlxapxep.SelectedValue.ToString() == "3")
            {
                Sort_TrangThai(ddltanggiam.SelectedValue.ToString());
            }
        }

        protected void btntim_Click(object sender, EventArgs e)
        {
            getdhbyngay();
            GetDH();
            //adddl();
            lbthongbao.Text = "";
        }

        private void getdhbyngay()
        {
            if (ddltungay.SelectedValue != "0" && ddldenngay.SelectedValue != "0")
            {
                dh = new BLLDonHang("", "1", Convert.ToDateTime(ddltungay.SelectedValue), Convert.ToDateTime(ddldenngay.SelectedValue), Ddltrangthai.SelectedValue.ToString()).GetByFilter();
            }
            else
            {
                dh = new BLLDonHang("", "0", Ddltrangthai.SelectedValue.ToString()).GetByFilter();
            }
        }
        private bool checkdate(string s)
        {
            DateTime value;
            if (DateTime.TryParse(s,out value))
            {
                return true;
            }
            return false;
        }
    }
}
